/*
 * Project Name: jmax
 * File Name: BaseInit.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.base.init;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Params;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author max.zheng
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-6-2 下午2:12:29
 */
public class BaseInit implements SysInitOperation{
	Log log = LogFactory.getLog(BaseInit.class);

	/* (非 Javadoc)
	 * initingToDb
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToDb(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToDb(BaseService<BaseModel> baseService)
	{
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "BaseInit","true");
		log.info("基础模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			initData(baseService);
			Params p = new Params("BaseInit", "true", "基础模块数据库初始化标记");
			baseService.save(p);
			log.info("基础模块数据库初始化......结束");
		}else
		{
			log.info("基础模块数据库已经初始化......完毕");
		}
		
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-6-2 下午2:24:04
	 * @param baseService
	 */
	private void initData(BaseService<BaseModel> baseService)
	{
		MenuFunction m1 = new MenuFunction();
		m1.setIconName("icon-save");
		m1.setNameCN("基础模块");
		m1.setOperationCode("base-manage");
		m1.setPageUrl(null);
		baseService.save(m1);
		
		MenuFunction m11 = new MenuFunction();
		m11.setIconName("icon-save");
		m11.setNameCN("行政区域");
		m11.setOperationCode("district-manage");
		m11.setPageUrl("district.do?district");
		m11.setParent(m1);
		baseService.save(m11);
		
		//初始化行政区域脚本
		initDistrict(baseService);
	}

	/**
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-6-19 下午1:11:23
	 */
	private void initDistrict(BaseService<BaseModel> baseService)
	{
		
		try {
			String rootPath = this.getClass().getResource("/").getPath();
			String filePath = rootPath + "district.sql";
			File file = new File(filePath);
			FileInputStream in = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(in,
					"UTF-8"));
			String sql;
			while ((sql = br.readLine()) != null) {
				if (0 != sql.length() && (sql.contains("INSERT")||sql.contains("insert"))) {
					String line = sql.trim();
					line = line.substring(0, line.length() - 1);
					log.info(line);
					baseService.executeUpdateBySql(line);
				}
			}
			br.close();
		} catch (Exception e) {
			log.error("初始化行政区域编码失败......");
		}
		
	}

	/* (非 Javadoc)
	 * initingToCache
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToCache(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToCache(BaseService<BaseModel> baseService)
	{
		
	}

	/* (非 Javadoc)
	 * getInitOrder
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @return
	 * @see com.xdtech.core.init.SysInitOperation#getInitOrder()
	 */
	@Override
	public int getInitOrder()
	{
		return 2;
	}

	/* (非 Javadoc)
	 * initingToWebApplication
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToWebApplication(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService)
	{
		// TODO Auto-generated method stub
		
	}

}
